package com.sun.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sun.common.Result;
import com.sun.common.SystemCrmlog;
import com.sun.entity.Category;
import com.sun.service.CategoryService;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @Project: hospitalDrugInformationManagement
 * @Author: 86178
 * @Date: 2023/2/7 10:32
 * @FileName: UserController
 * @Description: "分类管理 还未配置用户权限"
 */
@RestController
@RequestMapping("/category")
public class CategoryController {

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     * @param category
     * @return
     */
    @SystemCrmlog(title = "药品信息管理模块", type = "INSERT", description = "对药品分类进行新增")
    @RequiresAuthentication
    @PostMapping
    public Result<String> save(@RequestBody Category category) {
        categoryService.save(category);
        return Result.success("新增分类成功");
    }

    /**
     * 查询所有分类
     *
     * @param
     * @return
     */
    @RequiresAuthentication
    @GetMapping
    public Result<List<Category>> getList() {
        List<Category> list = categoryService.list();
        return Result.success(list);
    }

    /**
     * 分页查询
     *
     * @param pageNum
     * @param pageSize
     * @return
     */
    @RequiresAuthentication
    @GetMapping("/page")
    public Result<Page> page(int pageNum, int pageSize) {
        Page<Category> pageInfo = new Page<>(pageNum, pageSize);
        LambdaQueryWrapper<Category> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.orderByAsc(Category::getSort);
        categoryService.page(pageInfo, lambdaQueryWrapper);
        return Result.success(pageInfo);
    }

    /**
     * 根据id删除分类
     *
     * @param categoryId
     * @return
     */
    @SystemCrmlog(title = "药品信息管理模块", type = "DELETE", description = "删除某一药品分类")
    @RequiresAuthentication
    @DeleteMapping("/{categoryId}")
    public Result<String> delete(Long categoryId) {
        categoryService.remove(categoryId);
        return Result.success("分类信息删除成功");
    }

    /**
     * 根据id修改分类信息
     * @param category
     * @return
     */
    @SystemCrmlog(title = "药品信息管理模块", type = "UPDATE", description = "修改某一药品分类进行")
    @RequiresAuthentication
    @PutMapping
    public Result<String> update(@RequestBody Category category){
        categoryService.updateById(category);
        return Result.success("分类信息修改成功");
    }

}
